/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： BaseImpl.java
 * Date：18-11-5 上午10:54
 * Author: guomw
 */

package com.guomw.report.service.service.impl;

import com.guomw.report.service.entity.SystemManager;
import com.guomw.report.service.enums.RoleType;
import com.guomw.report.service.models.PageView;
import com.guomw.report.service.models.Searcher;
import com.guomw.report.service.repository.system.SystemManagerRepository;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;


/**
 * @author guomw
 * @date 2018/10/15
 */
@Service
public class BaseImpl {
    @Autowired
    private SystemManagerRepository systemManagerRepository;

    public void checkUser(Searcher searcher) {
        if (searcher.getUserId() > 0) {
            SystemManager manager = systemManagerRepository.findOne(searcher.getUserId());
            if (manager != null && manager.getRole().getRoleType() == RoleType.Default.getCode()) {
                //超级管理员身份，设置为-1
                searcher.setUserId(-1);
            }
        }
    }

    public  <T> PageView<T> getPageView(Page<T> page) {
        PageView<T> pageView = new PageView<>();
        pageView.setPageCount(page.getTotalPages());
        pageView.setTotalCount(page.getTotalElements());
        pageView.setList(page.getContent());
        return pageView;
    }
}
